import pandas as pd
from ETSDK import ETSDK
import datetime

if __name__ == "__main__":
    try:
        # ===========================================================================================================
        # 00 ）使用token进行身份认证
        # ===========================================================================================================
        sdk = ETSDK()
        sdk.auth("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
        
        # ===========================================================================================================
        # 01 ）股票因子相关操作
        # ===========================================================================================================
        df = sdk.get_stock_factor()

        var_FactorID = sdk.get_stock_factor_id(factor_code='test_stk_01')
        df = sdk.get_stock_factor_value_daily(factor_code='test_stk_01')

        # 01.1 - Stock - LF Factor manipulation
        sdk.create_stock_factor(parent_category='TestParent',
                                category='TestCat',
                                factor_code='test_stk_02',
                                description='sdk功能性测试',
                                remark='sdk功能性测试',
                                level='1',
                                factor_type='relative_value',
                                suggested_update_time='T0 22:00')

        sdk.update_stock_factor(factor_id=1701,
                                category_id=69,
                                factor_code='test_stk_01',
                                description='sdk test',
                                remark='sdk功能性测试',
                                level='2',
                                suggested_update_time='T0 23:00')

        sdk.save_stock_factor_value_DataFrame(factor_code="test_stk_01",
                                              factor_dataframe=pd.DataFrame(
                                                  {"TradingDay": [20221008, 20221125],
                                                   "StockCode": ["600010.SH", "000201.SZ"],
                                                   "FactorValue": [1.2345, 2.1234]}))

        sdk.delete_stock_factor_value_table(factor_code='test_stk_01')

        # 01.2 - Stock - HF Factor manipulation
        sdk.create_stock_factor(parent_category='TestParent',
                                category='TestCat',
                                factor_code='test_hf_stk_01',
                                description='sdk功能性测试',
                                remark='sdk功能性测试',
                                level='1',
                                factor_type='relative_value',
                                suggested_update_time='T0 22:00')

        sdk.save_hf_factor_value_DataFrame(factor_code='test_hf_stk_01',
                                           security_type='Stock',
                                           factor_dataframe=pd.DataFrame(
                                               {"TradingTime": ['2022-06-30 09:45:00', '2022-06-30 09:50:00'],
                                                "SecurityCode": ["600000.SH", "600000.SH"],
                                                "FactorValue": [1.2345, 2.1234]})
                                           )

        sdk.delete_stock_factor_value_table(factor_code='test_hf_stk_01')

        # ===========================================================================================================
        # 02 ）期货因子相关操作
        # ===========================================================================================================
        sdk.create_et_factor(asset_type='Future',
                             parent_category='TestParent',
                             category='TestCat',
                             factor_code='test_sdk_05',
                             description='sdk功能性测试',
                             remark='sdk功能性测试',
                             level='1',
                             factor_type='relative_value',
                             suggested_update_time='T0 22:00')

        var_FactorID = sdk.get_factor_id(asset_type='Future',
                                         factor_code='test_sdk_03')

        sdk.update_et_factor(factor_id=2508,
                             factor_code='test_sdk_03',
                             remark='sdk测试',
                             factor_type='relative_value',
                             suggested_update_time='T0 20:00')

        # 单次保存一个dataframe的因子数据
        sdk.save_future_factor_value_DataFrame(factor_code="test_sdk_05",
                                               factor_dataframe=pd.DataFrame(
                                                   {"TradingDay": [20211008, 20211125],
                                                    "FutureCode": ["IF2203", "IC2203"],
                                                    "FactorValue": [1.2345, 2.1234]}))

        # 查看保存好的数据
        df = sdk.get_future_factor_value_daily(factor_code='test_sdk_05')

        # 单次保存一个dataframe的因子数据
        sdk.save_future_factor_value_DataFrame(factor_code="test_sdk_05",
                                               factor_dataframe=pd.DataFrame(
                                                   {"TradingDay": [20211008, 20211125],
                                                    "FutureCode": ["IF2203", "IC2203"],
                                                    "FactorValue": [2.2345, 3.1234]}))

        # 删除指定因子数据，特定期货合约
        sdk.delete_future_factor_value_table(factor_code='test_sdk_05',
                                             future_code='IC2203')

        df = sdk.get_future_factor_value_daily(factor_code='test_sdk_05')

        # 删除指定因子的全部数据，所有期货合约
        sdk.delete_future_factor_value_table(factor_code='test_sdk_05')

        df = sdk.get_future_factor_value_daily(factor_code='test_sdk_05')

        sdk.delete_future_factor_value_table(factor_code='test_sdk_05',
                                             remove_meta=True)

        # 删除因子基本信息
        sdk.remove_et_factor(asset_type='Future',
                             factor_code='test_sdk_05')

        # ===========================================================================================================
        # 3）高频因子数据操作示例----------HF----------
        # ===========================================================================================================
        sdk.create_et_factor(asset_type='Future',
                     parent_category='TestParent',
                     category='TestCat',
                     factor_code='test_hf_fut_01',
                     description='sdk测试',
                     remark='sdk功能性测试',
                     level='1',
                     suggested_update_time='T0 22:00')

        sdk.save_hf_factor_value_DataFrame(factor_code='test_hf_fut_01',
                                           security_type='Future',
                                           factor_dataframe=pd.DataFrame(
                                               {"TradingTime": ['2022-06-30 09:45:00', '2022-06-30 09:50:00'],
                                                "SecurityCode": ["IF2207", "IF2207"],
                                                "FactorValue": [1.2345, 2.1234]}))

        df = sdk.get_future_factor_value_daily(factor_code='test_hf_fut_01')

        sdk.delete_future_factor_value_table(factor_code='test_hf_fut_01')

        # ===========================================================================================================
        # 4）因子评估
        # ===========================================================================================================
        # 获取因子评估信息
        # 获取全部因子评估信息
        df = sdk.get_factor_assessment()

        # 获取特定资产类型的因子评估信息
        df = sdk.get_factor_assessment(asset_type='Future')

        # 获取特定代码的因子评估信息（可能存在股票因子和期货因子中都存在代码为XXXXX的因子）
        df = sdk.get_factor_assessment(factor_code='ACCA')

        # 获取特定因子的评估信息
        df = sdk.get_factor_assessment(asset_type='Future', factor_code='test_sdk_03')

        # 保存/更新因子评估信息
        # 参数asset_type和factor_code是必填项，其它均是可选项；
        # a. 全部参数示例
        sdk.save_factor_assessment(asset_type='Future',
                                   factor_code='test_sdk_03',
                                   in_sample_start='2015-01-01',
                                   in_sample_end='2021-12-31',
                                   back_test_days=1586,
                                   back_test_avg_turnover=0.5824,
                                   back_test_sharp=1.6235,
                                   out_sample_end='2023-01-31',
                                   out_sample_days=402,
                                   out_sample_sharp=1.4624,
                                   is_valid=1,
                                   valid_until='9999-12-31',
                                   factor_type='relative_value')
        # b. 部分参数示例
        sdk.save_factor_assessment(asset_type='Future',
                                   factor_code='test_sdk_03',
                                   in_sample_start='2015-01-01',
                                   out_sample_end='2023-02-15',
                                   out_sample_days=410,
                                   out_sample_sharp=1.2143)

        sdk.save_factor_assessment(asset_type='Future',
                                   factor_code='test_sdk_03',
                                   is_valid=1)

        print("SUCCESS")
    except Exception as e:
        raise e
